# -*-coding:utf-8 -*-
# author: leemon
# datetime: 2020/9/15 15:41
import requests
import json

saas_ovpn_url = {
    'authSessionAPI': '/index.php?admin',
    'userAPI': '/include/grids.php',
}


class SaasOvpn:
    def __init__(self, url, admin_username, admin_password):
        self.url = url
        self.admin_username = admin_username
        self.admin_password = admin_password

    def login(self):
        params = {'admin_username': self.admin_username, 'admin_pass': self.admin_password, 'admin_login': 'Login'}
        req = requests.session()
        res = req.post('%s%s' % (self.url, saas_ovpn_url['authSessionAPI']), params)
        if res.status_code != 200:
            return False
        self.req = req
        return True

    def useradd(self, username, password='123456'):
        status = True
        msg = ''

        if not self.usersearch(username):
            useradd_params = {'user_id': username, 'user_pass': password, 'add_user': 'true'}
            res = self.req.post('%s%s' % (self.url, saas_ovpn_url['userAPI']), useradd_params)
            if res.status_code != 200:
                status = False
        else:
            status = False
            msg = '用户已存在： %s' % username

        return {'status': status, 'username': username, 'password': password, 'view_url': self.url, 'msg': msg, 'type': 'phpadmin'}

    def userdel(self, username):
        if self.usersearch(username):
            userdel_params = {'del_user_id': username, 'del_user': 'true'}
            res = self.req.post('%s%s' % (self.url, saas_ovpn_url['userAPI']), userdel_params)
            if res.status_code != 200:
                return False

            return True
        else:
            print('用户不存在： %s' % username)
            return False

    def userdisable(self, username):
        userdisable_params = {'name': 'user_enable', 'value': '0', 'pk': username, 'set_user': 'true'}
        res = self.req.post('%s%s' % (self.url, saas_ovpn_url['userAPI']), userdisable_params)
        if res.status_code != 200:
            return False

        return True

    def userenable(self, username):
        userenable_params = {'name': 'user_enable', 'value': '1', 'pk': username, 'set_user': 'true'}
        res = self.req.post('%s%s' % (self.url, saas_ovpn_url['userAPI']), userenable_params)
        if res.status_code != 200:
            return False

        return True

    def usersearch(self, username):
        res = self.req.get('%s%s?order=asc&select=user' % (self.url, saas_ovpn_url['userAPI']))
        if res.status_code == 200:
            json_res = json.loads(res.content)
            for user in json_res:
                if user['user_id'] == username:
                    return user
        else:
            return False

    def get_all_users(self):
        res = self.req.get('%s%s?order=asc&select=user' % (self.url, saas_ovpn_url['userAPI']))
        users_list = []

        if res.status_code == 200:
            json_res = json.loads(res.content)
            for user in json_res:
                user_dict = {'userid': user['user_id'], 'name': user['user_id'], 'email': user['user_mail'],
                             'organization': ''}
                if user['user_enable'] == '1':
                    user_dict['disabled'] = False
                else:
                    user_dict['disabled'] = True

                users_list.append(user_dict)

            return users_list
        else:
            return False


if __name__ == '__main__':
    pass